trigger OORFetchOwnerDetails on Notes__c (before Insert,before Update)
 {
 
    //Trigger Name:OORFetchOwnerDetails
    //Date:21st September 2011  
    //Author:Accenture IDC  
    //Requirement/Project Name:OOR Phase 2 
    //Requirement/Project Description:The Triggers fire on Insert or Update and if Owner has Changed.It Updates the Onwer's Manager Name and Owner's SFA Office. 
  
  Set<Id> user_id = new Set<Id>();
  List<User> users= New List<User>();
   for(Notes__c notes :Trigger.New) 
     {
       if(Trigger.isUpdate)
          {  
                 Notes__c note = Trigger.OldMap.get(notes.Id);
                     if(note .OwnerId!=notes.Ownerid)
                     {  
                         user_id.add(notes.OwnerId);
                     }
          } 
        if(Trigger.isInsert)
           {
                 user_id.add(notes.OwnerId);
           
           }   
     }
     if(user_id.size()>0)
     {
     users=[Select ManagerId,SFA_Office_Name__c,SFA_Office__c,ATTUID__c,AFrontier_Employee_ID__c from User where Id IN: user_id ];
     Map<Id,User> usrs = New Map<Id,User>(); 
             for(User usr:users) 
             {      
                 if(usr.ManagerId!=null){
                 usrs.put(usr.Id,usr); } 
             }   
            if(usrs.Size()>0)
            {      
             for(Notes__c note :Trigger.New) 
             { 
                   note.SFA_Office_Name__c=usrs.get(note.OwnerId).SFA_Office_Name__c;
                   note.SFA_Office_Code__c=usrs.get(note.OwnerId).SFA_Office__c; 
                   note.Primary_Owner_ATTUID__c=usrs.get(note.OwnerId).ATTUID__c;
                   note.Primary_Owner_AFrontier_Employee_ID__c=usrs.get(note.OwnerId).AFrontier_Employee_ID__c;
               if(usrs.get(note.OwnerId).ManagerId!=null)
                  {
                    try  
                     {  
                         note.User_Manager__c=usrs.get(note.OwnerId).ManagerID;
                      }  
                     catch(DMLException exe) 
                     {   
                         GeneralConstants.mailAnyExceptionToMe('OORFetchOwnerDetails','Trigger on Notes',exe);
                     }
                  }   
             } 
           }      
     }
 }